package com.cj.logc.simple

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.bnq.logc.Logc
import com.bnq.logc.ui.file.FileBrowserActivity
import com.bnq.logc.utils.*
import java.io.File
import java.io.FileWriter
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<TextView>(R.id.text_id).text = DeviceUtil.getUniqueId(this) +"\n" +
                "SDTotalSize:${FileSizeUtil.formatFileSize(StorageUtil.getSDTotalSize())}\n" +
                "SdAvailableSize:${FileSizeUtil.formatFileSize(StorageUtil.getSdAvailableSize())}\n" +
                "RomTotalSize:${FileSizeUtil.formatFileSize(StorageUtil.getRomTotalSize())}\n" +
                "RomAvailableSize:${FileSizeUtil.formatFileSize(StorageUtil.getRomAvailableSize())}\n"

    }

    fun btnLog(view:View){
        thread {
            for(i in 1..100){
                Logc.i("Thread","测试日志数据子线程")
            }
        }
        for(i in 1..100){
            Logc.w("Main","测试日志数据主线程,警告记录")
        }
    }

    fun btnLogError(view: View){
        Logc.e("Main","测试日志数据主线程,错误记录")
    }

    fun btnLogSimple(view: View) {
        val logPath= File(externalCacheDir,"log")
        val logFile = File(logPath,"${System.currentTimeMillis()}.log")
        if(!logPath.exists()){
            logPath.mkdirs()
        }

        if(!logFile.exists()){
            logFile.createNewFile()
        }
        FileWriter(logFile,true).use { out ->
            out.write("Simple Cache Log 测试数据: ${System.currentTimeMillis()}")
            out.write("\n")
        }
    }

    fun btnUploadFile(view: View) {
        val logPath= File(externalCacheDir,"log")
        val logFile = File(logPath,"${System.currentTimeMillis()}.log")
        if(!logPath.exists()){
            logPath.mkdirs()
        }

        if(!logFile.exists()){
            logFile.createNewFile()
        }
        FileWriter(logFile,true).use { out ->
            out.write("上传文件 测试: ${System.currentTimeMillis()}")
            out.write("\n")
        }
        Logc.getTmpUploadFile().uploadFile(listOf(logFile), listOf("logc/tmp/test_upload/${logFile.name}")){
            runOnUiThread{
                findViewById<TextView>(R.id.text_id).text = it.joinToString { it.file.absolutePath + " : " + it.status + "\n" }
            }
        }
    }

    fun btnFileBrowser(view: View){
        val intent = Intent(this, FileBrowserActivity::class.java)
        startActivity(intent)
    }
}